<?php

class HCategoryRobot {
	// Variables
	public $CategoryTree;
	public $CategoryList;
	public $TreeCount;
	
	//__construct
	function __construct() {
		$this->CategoryTree = '';
		$this->CategoryList = HSql::fetch2Array(HSql::getData('portal_pages',"WHERE portal_id = '".XPORTAL_ID."'"));
		if ($this->CategoryList == 0) $this->CategoryList = array(); // Avoid zero return
		
		for ($i = 0; $i < count($this->CategoryList); $i++) {
			$this->CategoryList[$i]['NodeID'] = null;
			$this->CategoryList[$i]['NodeName'] = null;
		}
		$this->CategoryList[0]['NodeID'] = 0;
		$this->CategoryList[0]['NodeName'] = "Root";
		
		$this->TreeCount = 1;
	}
	
	function GetRootID($nodeID) {
		$a = HSql::fetch2Array(HSql::getData('portal_pages',"WHERE id = '".$nodeID."'"));
		if (count($a) != 0) return $a[0]['parent_id'];
		else return null;
	}
	
	function GetNodeName($nodeID) {
		$a = HSql::fetch2Array(HSql::getData('portal_pages',"WHERE id = '".$nodeID."'"));
		if (count($a) != 0) return $a[0]['page_alias'];
		else return null;
	}
	
	function HasChild($nodeID) {
		$a = HSql::fetch2Array(HSql::getData('portal_pages',"WHERE parent_id = '".$nodeID."'"));
		if (count($a) == 0) return false;
		else return true;
	}
	
	function BuildTree($nodeID) {
		$a = HSql::fetch2Array(HSql::getData('portal_pages',"WHERE parent_id = '".$nodeID."' AND portal_id = '".XPORTAL_ID."'"));
		// Check Null
		if ($a == 0) $count = 0;
		else $count = count($a);
		// Loop
		for ($i = 0; $i < $count; $i++) {
			if (self::HasChild($a[$i]['id'])) {
				$this->CategoryTree .= "<li><a href=\"#\" onclick=\"ajaxpage('includes/page_tree.php?id=".$a[$i]['id']."&p=".$_GET['p']."&mid=2','pnDetails')\">".self::GetNodeName($a[$i]['id'])."</a>";
                $this->CategoryTree .= "<ul>";
                self::BuildTree($a[$i]['id']);
                $this->CategoryTree .= "</ul>";
                $this->CategoryTree .= "</li>";
			}
			else {
				$this->CategoryTree .= "<li><a href=\"#\" onclick=\"ajaxpage('includes/page_tree.php?id=".$a[$i]['id']."&p=".$_GET['p']."&mid=2','pnDetails')\">".self::GetNodeName($a[$i]['id'])."</a>";
                $this->CategoryTree .= "</li>";
			}
		}
	}
	
	function BuildTreeList($nodeID, $PathString) {
		$a = HSql::fetch2Array(HSql::getData('portal_pages',"WHERE parent_id = '".$nodeID."' AND portal_id = '".XPORTAL_ID."' ORDER BY id ASC"));
		if ($a == 0) $count = 0;
		else $count = count($a);
		
		for ($i = 0; $i < $count; $i++) {
			$TempPath = '';
			if (self::HasChild($a[$i]['id'])) {
				$TempPath = $PathString.' -> '.self::GetNodeName($a[$i]['id']);
				$this->CategoryList[$this->TreeCount]['NodeID'] = $a[$i]['id'];
				$this->CategoryList[$this->TreeCount]['NodeName'] = $TempPath;
				$this->TreeCount++;
				self::BuildTreeList($a[$i]['id'],$TempPath);
			}
			else {
				$TempPath = $PathString.' -> '.self::GetNodeName($a[$i]['id']);
				$this->CategoryList[$this->TreeCount]['NodeID'] = $a[$i]['id'];
				$this->CategoryList[$this->TreeCount]['NodeName'] = $TempPath;
				$this->TreeCount++;
			}
		}
	}
	
	
	// SQL Functions
	function CreateCategory($Name, $Root) {
		$query = "INSERT INTO portal_pages(name,parent_id) VALUES('".$Name."','".$Root."')";
		@mysql_query($query) or die(mysql_error());
	}
	
	function UpdateCategory($ID, $Name, $RootID) {
		$query = "UPDATE portal_pages SET name = '".$Name."', parent_id = '".$RootID."' WHERE id = '".$ID."'";
		@mysql_query($query) or die(mysql_error());
	}
}

?>